<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备 ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入设备 ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="equipmentModel">
        <el-input
          v-model="queryParams.equipmentModel"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实时流量" prop="realTimeFlow">
        <el-input
          v-model="queryParams.realTimeFlow"
          placeholder="请输入实时流量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实时压力" prop="realTimePressure">
        <el-input
          v-model="queryParams.realTimePressure"
          placeholder="请输入实时压力"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据更新时间" prop="dataUpdateTime">
        <el-date-picker clearable
          v-model="queryParams.dataUpdateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择数据更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="数据刷新频率" prop="dataRefreshFrequency">
        <el-input
          v-model="queryParams.dataRefreshFrequency"
          placeholder="请输入数据刷新频率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="历史数据 ID" prop="historicalDataId">
        <el-input
          v-model="queryParams.historicalDataId"
          placeholder="请输入历史数据 ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="历史数据时间" prop="historicalDataTime">
        <el-date-picker clearable
          v-model="queryParams.historicalDataTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择历史数据时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="历史流量数据" prop="historicalFlowData">
        <el-input
          v-model="queryParams.historicalFlowData"
          placeholder="请输入历史流量数据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="历史压力数据" prop="historicalPressureData">
        <el-input
          v-model="queryParams.historicalPressureData"
          placeholder="请输入历史压力数据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上次更新时间" prop="lastUpdateTime">
        <el-date-picker clearable
          v-model="queryParams.lastUpdateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择上次更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dispatchConfiguration:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dispatchConfiguration:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dispatchConfiguration:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dispatchConfiguration:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dispatchConfigurationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="组态图 ID" align="center" prop="configurationId" />
      <el-table-column label="组态图类型" align="center" prop="configurationType" />
      <el-table-column label="三维模型数据" align="center" prop="threeDModelData" />
      <el-table-column label="设备 ID" align="center" prop="equipmentId" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="设备类型" align="center" prop="equipmentType" />
      <el-table-column label="设备型号" align="center" prop="equipmentModel" />
      <el-table-column label="额定参数" align="center" prop="ratedParameters" />
      <el-table-column label="运行状态" align="center" prop="operationStatus" />
      <el-table-column label="实时流量" align="center" prop="realTimeFlow" />
      <el-table-column label="实时压力" align="center" prop="realTimePressure" />
      <el-table-column label="水质指标" align="center" prop="waterQualityIndex" />
      <el-table-column label="数据更新时间" align="center" prop="dataUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据刷新频率" align="center" prop="dataRefreshFrequency" />
      <el-table-column label="报警阈值" align="center" prop="alarmThreshold" />
      <el-table-column label="报警状态" align="center" prop="alarmStatus" />
      <el-table-column label="报警信息" align="center" prop="alarmMessage" />
      <el-table-column label="历史数据 ID" align="center" prop="historicalDataId" />
      <el-table-column label="历史数据时间" align="center" prop="historicalDataTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.historicalDataTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="历史流量数据" align="center" prop="historicalFlowData" />
      <el-table-column label="历史压力数据" align="center" prop="historicalPressureData" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="上次更新时间" align="center" prop="lastUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="操作类型" align="center" prop="operationType" />
      <el-table-column label="历史水质数据" align="center" prop="historicalWaterQualityData" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dispatchConfiguration:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dispatchConfiguration:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改组态工艺对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="三维模型数据" prop="threeDModelData">
          <el-input v-model="form.threeDModelData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备 ID" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入设备 ID" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备型号" prop="equipmentModel">
          <el-input v-model="form.equipmentModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="额定参数" prop="ratedParameters">
          <el-input v-model="form.ratedParameters" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="实时流量" prop="realTimeFlow">
          <el-input v-model="form.realTimeFlow" placeholder="请输入实时流量" />
        </el-form-item>
        <el-form-item label="实时压力" prop="realTimePressure">
          <el-input v-model="form.realTimePressure" placeholder="请输入实时压力" />
        </el-form-item>
        <el-form-item label="水质指标" prop="waterQualityIndex">
          <el-input v-model="form.waterQualityIndex" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="数据更新时间" prop="dataUpdateTime">
          <el-date-picker clearable
            v-model="form.dataUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择数据更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数据刷新频率" prop="dataRefreshFrequency">
          <el-input v-model="form.dataRefreshFrequency" placeholder="请输入数据刷新频率" />
        </el-form-item>
        <el-form-item label="报警阈值" prop="alarmThreshold">
          <el-input v-model="form.alarmThreshold" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报警信息" prop="alarmMessage">
          <el-input v-model="form.alarmMessage" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="历史数据 ID" prop="historicalDataId">
          <el-input v-model="form.historicalDataId" placeholder="请输入历史数据 ID" />
        </el-form-item>
        <el-form-item label="历史数据时间" prop="historicalDataTime">
          <el-date-picker clearable
            v-model="form.historicalDataTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择历史数据时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="历史流量数据" prop="historicalFlowData">
          <el-input v-model="form.historicalFlowData" placeholder="请输入历史流量数据" />
        </el-form-item>
        <el-form-item label="历史压力数据" prop="historicalPressureData">
          <el-input v-model="form.historicalPressureData" placeholder="请输入历史压力数据" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="上次更新时间" prop="lastUpdateTime">
          <el-date-picker clearable
            v-model="form.lastUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="历史水质数据" prop="historicalWaterQualityData">
          <el-input v-model="form.historicalWaterQualityData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDispatchConfiguration, getDispatchConfiguration, delDispatchConfiguration, addDispatchConfiguration, updateDispatchConfiguration } from "@/api/system/dispatchConfiguration";

export default {
  name: "DispatchConfiguration",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组态工艺表格数据
      dispatchConfigurationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configurationType: null,
        threeDModelData: null,
        equipmentId: null,
        equipmentName: null,
        equipmentType: null,
        equipmentModel: null,
        ratedParameters: null,
        operationStatus: null,
        realTimeFlow: null,
        realTimePressure: null,
        waterQualityIndex: null,
        dataUpdateTime: null,
        dataRefreshFrequency: null,
        alarmThreshold: null,
        alarmStatus: null,
        alarmMessage: null,
        historicalDataId: null,
        historicalDataTime: null,
        historicalFlowData: null,
        historicalPressureData: null,
        creator: null,
        lastUpdateTime: null,
        updater: null,
        operationType: null,
        historicalWaterQualityData: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询组态工艺列表 */
    getList() {
      this.loading = true;
      listDispatchConfiguration(this.queryParams).then(response => {
        this.dispatchConfigurationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configurationId: null,
        configurationType: null,
        threeDModelData: null,
        equipmentId: null,
        equipmentName: null,
        equipmentType: null,
        equipmentModel: null,
        ratedParameters: null,
        operationStatus: null,
        realTimeFlow: null,
        realTimePressure: null,
        waterQualityIndex: null,
        dataUpdateTime: null,
        dataRefreshFrequency: null,
        alarmThreshold: null,
        alarmStatus: null,
        alarmMessage: null,
        historicalDataId: null,
        historicalDataTime: null,
        historicalFlowData: null,
        historicalPressureData: null,
        creator: null,
        lastUpdateTime: null,
        updater: null,
        operationType: null,
        historicalWaterQualityData: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configurationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加组态工艺";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configurationId = row.configurationId || this.ids
      getDispatchConfiguration(configurationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改组态工艺";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configurationId != null) {
            updateDispatchConfiguration(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDispatchConfiguration(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configurationIds = row.configurationId || this.ids;
      this.$modal.confirm('是否确认删除组态工艺编号为"' + configurationIds + '"的数据项？').then(function() {
        return delDispatchConfiguration(configurationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dispatchConfiguration/export', {
        ...this.queryParams
      }, `dispatchConfiguration_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
